﻿using CfgTable;
using IQIGame.Onigao.Framework;

namespace IQIGame.Onigao.GamePlay
{
    public enum QuestState
    {
        None = 0,
        Activating = 1,
        CanCommit = 2,
        Success = 3,
        Fails = 4,
    }

    public class UD_OneQuest : IReusableClass
    {
        public int cid { get; private set; }

        private QuestModuleData quest => GameDataCenter.PlayerData.quest;
        public QuestData questData { get { quest.TryGetValue(cid, out var data); return data; } }

        public bool isInSuccess => this.quest.IsInSuccess(cid);
        public bool isInFail => this.quest.IsInFail(cid);
        public bool isActivating => this.questState == QuestState.Activating;
        /// <summary>
        /// 是否可提交
        /// </summary>
        public bool isCanCommit => this.questState == QuestState.CanCommit;
        private bool isTrackQuest => IsTrackQuest(cid);
        public bool isCanTrack => !this.isTrackQuest && this.isActivating;
        public bool isTracking => this.isTrackQuest && this.isActivating;

        private CfgQuest _cfgData = null;
        public CfgQuest cfgData
        {
            get
            {
                if (_cfgData == null || _cfgData.Id != cid)
                    _cfgData = TableCenter.quest.Get(cid);
                return _cfgData;
            }
        }

        /// <summary>
        /// 当前任务状态
        /// </summary>
        public QuestState questState => GetQuestState(cid);

        public UD_OneQuest Sync(int cid)
        {
            this.cid = cid;
            return this;
        }

        public QuestState GetQuestState(int cid)
        {
            if (quest.TryGetValue(cid, out var data))
            {
                if (data.isCanCommit)
                    return QuestState.CanCommit;
                return QuestState.Activating;
            }
            if (quest.IsInSuccess(cid))
                return QuestState.Success;
            if (quest.IsInFail(cid))
                return QuestState.Fails;
            return QuestState.None;
        }

        private bool IsTrackQuest(int cid)
        {
            int trackQuestCid = GameDataCenter.PlayerData.quest.trackQuestCid;
            if (trackQuestCid == 0)
                return false;
            if (trackQuestCid == this.cid)
                return true;
            foreach (var childCid in this.cfgData.ChildQuests)
            {
                if (trackQuestCid == childCid)
                    return true;
            }
            return false;
        }

        public void Clear()
        {
            this.cid = 0;
            this._cfgData = null;
        }

        #region 对象池
        public uint MaxStore => 10;

        public void OnReset()
        {
            this.Clear();
        }

        public void Put2Pool()
        {
            ClassPool.Put(this);
        }

        static public UD_OneQuest NewFromPool()
        {
            return ClassPool<UD_OneQuest>.Get();
        }
        #endregion
    }
}
